package com.its.us.mq.rocketmq;

import org.apache.rocketmq.acl.common.AclClientRPCHook;
import org.apache.rocketmq.acl.common.SessionCredentials;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.RPCHook;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @Author: master
 * @Date: 2020/11/29 16:36
 * @Version: 1.0
 */
public class TestMq {
    public static void main(String[] args) throws MQClientException, InterruptedException {
//        DefaultMQProducer producer = new DefaultMQProducer("store-group", getAclRPCHook());
//        DefaultMQProducer producer = new DefaultMQProducer();
        DefaultMQProducer producer = new DefaultMQProducer("groupA");
        producer.setNamesrvAddr("127.0.0.1:9876;127.0.0.1:9877");
        producer.start();
        for (int i = 0; i < 1; i++) {
            try {
//                Message msg = new Message("topicA" ,"TagA" , ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                Message msg = new Message("store-topic" ,"tag-a" , ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult sendResult = producer.send(msg);
                System.out.printf("%s%n", sendResult);
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(1000);
            }
        }
        producer.shutdown();
    }

    private static RPCHook getAclRPCHook(){
        return new AclClientRPCHook(new SessionCredentials("rocketmq","root1234569"));
    }
}
